package com.jgc.locks;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by Administrator on 2022/3/23.
 * 三种停止线程的方法
 */
public class InterruptDemo {
    static volatile boolean isStop = false;
    static AtomicBoolean atomicBoolean = new AtomicBoolean(false);
    public static void main(String[] args) {
        System.out.println(1);
    }
    public void m1(){
        new Thread(()->{
            while (true){
                if(isStop){
                    System.out.println("--------isstop = true程序结束");
                    break;
                }
                System.out.println("-----hello isstop");
            }
        },"t1").start();

        new Thread(() -> isStop =true,"t2").start();
    }
    public void m2(){
            new Thread(()->{
                while (true){
                    if(atomicBoolean.get()){
                        System.out.println("--------atomicBoolean = true程序结束");
                        break;
                    }
                    System.out.println("-----hello atomicBoolean");
                }
            },"t1").start();
            new Thread(() -> atomicBoolean.set(true),"t2").start();
    }

    public void m3(){
        Thread t1 = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("--------isInterrupted = true程序结束");
                    break;
                }
                System.out.println("-----hello isInterrupted");
            }
        }, "t1");
        t1.start();
        new Thread(() ->t1.interrupt() ,"t2").start();
    }
}
